package com.gz.controller;

import java.util.Date;
import java.util.List;

import com.gz.common.IntegralrecordService;
import com.gz.common.UserService;
import com.gz.common.model.Integralrecord;
import com.gz.common.model.User;
import com.gz.utils.Response;
import com.jfinal.core.Controller;

/**
  *  积分记录
 * @author Administrator
 *
 */

public class IntegralrecordController extends Controller {

	/**
	 *    积分提现记录
	 * @author 绝爷他Father
	 * @param userId
	 * @param integral
	 * @return 1006:账户积分不足
	 * @return 1007:正在审核
	 * @return 1008:提现失败
	 * @URL http://localhost:8080/cms-api/integralrecord/getIntegral
	 */
	public void getIntegral () {
		// 获取提现积分
		Integer integral = getParaToInt("integral");
		/*获取用户信息*/
		Integer userId = getParaToInt("userId");
		User user = UserService.getService().findUserId(userId);
		// 判断提现的积分是否大于用户已有的积分
		if(integral>user.getAmountS()) {
			renderJson(Response.responseJson(1006,"账户积分不足！"));
			return;
		}else {	
			Integralrecord integralrecord=new Integralrecord();
			integralrecord.setUseintegral(integral);
			integralrecord.setUserid(user.getId());
			// 1=分润、2=提现、3=优惠卷消费
			integralrecord.setType(2);
			// 1=成功 2=失败 0=默认审核中
			integralrecord.setStatus(0);
			integralrecord.setCreatetime(new Date());
			// 修改用户提现积分
			user.setAmountS(user.getAmountS()-integral);
			UserService.getService().modifyUserGrade(user);
			//计算提现后所得金额
			integralrecord.setMoeny(integral*0.0009);
			// 新增积分提现记录
			boolean result=IntegralrecordService.getService().xinZengJiFeng(integralrecord);
			if(result) {
				renderJson(Response.responseJson(1007,"正在审核！",0));
				return;
			}else {
				renderJson(Response.responseJson(1008,"提现失败！"));
				return;
			}
		}
	}
	
	/**
	 * 根据用户id分页查询积分提现记录
	 * 
	 * @param userId     用户id
	 * @return code:1006-成功：1007-失败，msg:查询成功，data：积分提现集合
	 * @author px的Father
	 * @http: http://localhost:8080/cms-api/integralrecord/findIntegralrecordList
	 */
	public void findIntegralrecordList() {
		/*获取用户信息*/
		Integer userId = getParaToInt("userId");
		// 查询用户积分提现记录集合
		List<Integralrecord> integralrecordList=IntegralrecordService.getService().findIntegralrecordList(userId);
		// 返回积分提现记录结果
		renderJson(Response.responseJson(1006,"查询积分提现记录成功！",integralrecordList));
	}
	

	/**
	 * 根据用户id分页查询积分提现记录
	 * 
	 * @param userId     用户id
	 * @return 
	 * @author sfw
	 * @http: http://localhost:8080/cms-api/integralrecord/findTXrecordList
	 */

	public void findTXrecordList(){
		Integer userId = getParaToInt("userId");
		List<Integralrecord> TXrecordList=IntegralrecordService.getService().findTXrecordList(userId);
		if(TXrecordList!=null&&TXrecordList.size()>0) {
			renderJson(Response.responseJson(1006,"查询成功！",TXrecordList));
		}else {
			renderJson(Response.responseJson(1005,"查询结果为空！"));
		}
	}
	
//	=======================
//	/**
//	 * @author YinChile
//	 * 积分记录查询
//	 * http://localhost:8081/cms-admin/Integral/findIntegralRecord
//	 */
//	public void findIntegralRecord() {
//		Integer username1=getParaToInt("username1");
//		Integer username2=getParaToInt("username2");
//		String order = getPara("order");
//		renderJson(Response.responseJson(0, "成功",IntegralrecordService.getService().findIntegralrecords(username1,order,username2)));
//		
//	}
}
